package com.cs.springcloud.controller;


import com.cs.springcloud.entities.Dept;
import com.cs.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * DeptController
 *
 * @author zzc
 * @version 1.00
 * @Date 2019/3/17-22:06
 */
@RestController
public class DeptController {

    @Autowired
    DeptService deptService;
//    @Autowired
//    DiscoveryClient client;

    @GetMapping(value = "/haha")
    public Object discover() {
//        List<String> list = client.getServices();
//        System.out.println("==========="+list);

        return "hahaahah";
    }

    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept) {
        return deptService.addDept(dept);
    }

    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "processHystrixList", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                    value = "1000")})
    public Dept get(@PathVariable("id") Long id) {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return deptService.findById(id);
    }

    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public List<Dept> list() {
        return deptService.findAll();
    }

    public Dept processHystrixList(@PathVariable("id") Long id) {
        return new Dept().setDname("调用get方法出现毛病，熔断了" + id).setDeptno(1L);
    }
}
